package com.codeoss.framework.webide.service.impl;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.codeoss.framework.context.YunContext;
import com.codeoss.framework.data.metadata.Column;
import com.codeoss.framework.data.metadata.Query;
import com.codeoss.framework.data.metadata.Restrictions;
import com.codeoss.framework.data.metadata.Table;
import com.codeoss.framework.data.store.ITableStore;
import com.codeoss.framework.db.util.ValueNamePairUtil;
import com.codeoss.framework.impexp.ExportManager;
import com.codeoss.framework.util.ValueNamePair;
import com.codeoss.framework.webide.request.DevWorkflowDesignerRequest;
import com.codeoss.framework.webide.response.DevWorkflowDesignerResponse;
import com.codeoss.framework.webide.service.IDevWorkflowDesignerService;
import com.codeoss.module.ir.model.IrModel;
import com.codeoss.module.ir.model.IrWorkflow;
import com.codeoss.module.ir.service.IIrModelService;
import com.codeoss.module.ir.service.IIrWorkflowService;
import com.codeoss.module.workflow.service.IWorkflowWorkflowService;

@Service
public class DevWorkflowDesignerService implements IDevWorkflowDesignerService {
	@Autowired
	private IIrWorkflowService irWorkflowService;
	@Autowired
	private IWorkflowWorkflowService workflowWorkflowService;
	@Autowired
	private ExportManager exportManager;
	@Autowired
	private ITableStore tableStore;
	@Autowired
	private IIrModelService irModelService;
	@Autowired
	private ValueNamePairUtil valueNamePairUtil;
	@Override
	public DevWorkflowDesignerResponse init(String code) {
		// TODO Auto-generated method stub
		DevWorkflowDesignerResponse devWorkflowDesignerResponse = new DevWorkflowDesignerResponse();
		YunContext yunContext=YunContext.getCurrentContext();
		QueryWrapper<IrWorkflow> irWorkflowQuery = new QueryWrapper<IrWorkflow>();
		irWorkflowQuery.eq("code", code);
		IrWorkflow irWorkflow = irWorkflowService.getOne(irWorkflowQuery);
		devWorkflowDesignerResponse.setWorkflowCode(code);
		devWorkflowDesignerResponse.setName(irWorkflow.getName());
		devWorkflowDesignerResponse.setFormCode(irWorkflow.getFormCode());
		devWorkflowDesignerResponse.setMobileFormCode(irWorkflow.getMobileFormCode());
		devWorkflowDesignerResponse.setWorkflowJson(irWorkflow.getJsonContent());
		devWorkflowDesignerResponse.setCurrentElementId(irWorkflow.getCurrentElementId());
		devWorkflowDesignerResponse.setIrModelId(irWorkflow.getIrModelId());
		devWorkflowDesignerResponse.setDisplayColumnList(irWorkflow.getDisplayColumnList());
		
		Table modelTable=tableStore.get("ir_model");
		List<ValueNamePair> tableList = valueNamePairUtil.getValueNamePairList(yunContext, modelTable, null, true, true);
		devWorkflowDesignerResponse.setTableList(tableList);
		Long irModelId=irWorkflow.getIrModelId();
		if(irModelId!=null) {
			IrModel currentMode=irModelService.getById(irModelId);
			Table table=tableStore.get(currentMode.getTableName());
			List<Column> columnList=table.getAllColumnList();
			List<ValueNamePair> pairList=new ArrayList<ValueNamePair>();
			devWorkflowDesignerResponse.setColumnList(pairList);
			for(int i=0;i<columnList.size();i++) {
				Column column=columnList.get(i);
				ValueNamePair pair = new ValueNamePair(column.getName() + "(" + column.getColumnName() + ")",column.getColumnName());
				pairList.add(pair);
			}
		}
		return devWorkflowDesignerResponse;
	}

	@Override
	@Transactional
	public DevWorkflowDesignerResponse dataSave(DevWorkflowDesignerRequest request) {
		// TODO Auto-generated method stub
		DevWorkflowDesignerResponse devWorkflowDesignerResponse = new DevWorkflowDesignerResponse();
		QueryWrapper<IrWorkflow> irWorkflowQuery = new QueryWrapper<IrWorkflow>();
		irWorkflowQuery.eq("code", request.getCode());
		IrWorkflow irWorkflow = irWorkflowService.getOne(irWorkflowQuery);
		irWorkflow.setJsonContent(request.getWorkflowJson());
		irWorkflow.setCurrentElementId(request.getCurrentElementId());
		irWorkflow.setName(request.getName());
		irWorkflow.setFormCode(request.getFormCode());
		irWorkflow.setMobileFormCode(request.getMobileFormCode());
		irWorkflow.setIrModelId(request.getIrModelId());
		irWorkflow.setDisplayColumnList(request.getDisplayColumnList());
		irWorkflowService.update(irWorkflow);
		return devWorkflowDesignerResponse;
	}

	@Override
	@Transactional
	public DevWorkflowDesignerResponse deploy(String code) {
		// TODO Auto-generated method stub
		DevWorkflowDesignerResponse devWorkflowDesignerResponse = new DevWorkflowDesignerResponse();
		workflowWorkflowService.deploy(code);
		return devWorkflowDesignerResponse;
	}

	@Override
	public DevWorkflowDesignerResponse dataExport(String code) throws IOException {
		// TODO Auto-generated method stub
		DevWorkflowDesignerResponse devWorkflowDesignerResponse = new DevWorkflowDesignerResponse();
		YunContext yunContext=YunContext.getCurrentContext();
		Table table=tableStore.get("ir_workflow");
		Query query=new Query();
		query.add(Restrictions.eq("code", code));
		ByteArrayOutputStream os=new ByteArrayOutputStream(); 
		exportManager.exportProperties(yunContext, table, query, os);
		String fileContent = new String(os.toByteArray(), "utf-8");
		String fileName = "workflow_" + code + ".prop";
		devWorkflowDesignerResponse.setFileContent(fileContent);
		devWorkflowDesignerResponse.setFileName(fileName);
		return devWorkflowDesignerResponse;
	}

	@Override
	public DevWorkflowDesignerResponse onTableSelectChangeHandler(Long irModelId) {
		// TODO Auto-generated method stub
		DevWorkflowDesignerResponse devWorkflowDesignerResponse = new DevWorkflowDesignerResponse();
		IrModel currentMode=irModelService.getById(irModelId);
		Table table=tableStore.get(currentMode.getTableName());
		List<Column> columnList=table.getAllColumnList();
		List<ValueNamePair> pairList=new ArrayList<ValueNamePair>();
		devWorkflowDesignerResponse.setColumnList(pairList);
		for(int i=0;i<columnList.size();i++) {
			Column column=columnList.get(i);
			ValueNamePair pair = new ValueNamePair(column.getName() + "(" + column.getColumnName() + ")",column.getColumnName());
			pairList.add(pair);
		}
		return devWorkflowDesignerResponse;
	}

}
